package com.ms3ds.mineInfo.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ms3ds.common.core.utils.DateUtils;
import com.ms3ds.common.core.utils.StringUtils;
import com.ms3ds.common.security.utils.SecurityUtils;
import com.ms3ds.mineInfo.domain.MineUser;
import com.ms3ds.mineInfo.api.domain.OreType;
import com.ms3ds.mineInfo.domain.UserMineRel;
import com.ms3ds.mineInfo.mapper.OreTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ms3ds.mineInfo.mapper.MineMapper;
import com.ms3ds.mineInfo.api.domain.Mine;
import com.ms3ds.mineInfo.service.IMineService;

/**
 * 矿山管理Service业务层处理
 * 
 * @author zlw
 * @date 2025-09-16
 */
@Service
public class MineServiceImpl implements IMineService 
{
    @Autowired
    private MineMapper mineMapper;

    @Autowired
    private OreTypeMapper oreTypeMapper;

    /**
     * 查询矿山管理
     * 
     * @param id 矿山管理主键
     * @return 矿山管理
     */
    @Override
    public Mine selectMineById(Long id)
    {
        return mineMapper.selectMineById(id);
    }

    /**
     * 查询矿山管理列表
     * 
     * @param mine 矿山管理
     * @return 矿山管理
     */
    @Override
    public List<Mine> selectMineList(Mine mine)
    {
        List<Mine> mines = mineMapper.selectMineList(mine);
        //获取矿种名称
        for (Mine m : mines) {
            String minedMinerals = m.getMinedMinerals();
            if(!StringUtils.isEmpty(minedMinerals)){
                String[] oreTypeList = minedMinerals.split(",");
                StringBuilder show = new StringBuilder();
                for(String oreType : oreTypeList){
                    OreType type = oreTypeMapper.selectOreTypeById(Long.valueOf(oreType));
                    if(!StringUtils.isEmpty(show.toString())){
                        show.append(",");
                    }
                    show.append(type.getOreTypeName());
                }
                m.setShowMinedMinerals(show.toString());
            }
        }
        return  mines;
    }

    @Override
    public List<Mine> selectMineListByUserId(Mine mine, Long userId) {
        List<Mine> mines = mineMapper.selectMineListByUserId(mine,userId);
        //获取矿种名称
        for (Mine m : mines) {
            String minedMinerals = m.getMinedMinerals();
            if(!StringUtils.isEmpty(minedMinerals)){
                String[] oreTypeList = minedMinerals.split(",");
                StringBuilder show = new StringBuilder();
                for(String oreType : oreTypeList){
                    OreType type = oreTypeMapper.selectOreTypeById(Long.valueOf(oreType));
                    if(!StringUtils.isEmpty(show.toString())){
                        show.append(",");
                    }
                    show.append(type.getOreTypeName());
                }
                m.setShowMinedMinerals(show.toString());
            }
        }
        return  mines;
    }

    @Override
    public List<MineUser> selectAllocatedList(MineUser user)
    {
        return mineMapper.selectAllocatedList(user);
    }

    /**
     * 根据条件分页查询未分配用户角色列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
    public List<MineUser> selectUnallocatedList(MineUser user)
    {
        return mineMapper.selectUnallocatedList(user);
    }

    /**
     * 批量选择授权用户矿山
     *
     * @param mineId 角色ID
     * @param userIds 需要授权的用户数据ID
     * @return 结果
     */
    @Override
    public int insertAuthUsers(Long mineId, Long[] userIds)
    {
        // 新增用户与角色管理
        List<UserMineRel> list = new ArrayList<UserMineRel>();
        for (Long userId : userIds)
        {
            UserMineRel um = new UserMineRel();
            um.setUserId(userId);
            um.setMineId(mineId);
            list.add(um);
        }
        return mineMapper.batchUserMine(list);
    }

    /**
     * 取消授权用户角色
     *
     * @param userMineRel 用户和角色关联信息
     * @return 结果
     */
    @Override
    public int deleteAuthUser(UserMineRel userMineRel)
    {
        return mineMapper.deleteUserMineInfo(userMineRel);
    }

    /**
     * 批量取消授权用户矿山
     *
     * @param mineId 矿山ID
     * @param userIds 需要取消授权的用户数据ID
     * @return 结果
     */
    @Override
    public int deleteAuthUsers(Long mineId, Long[] userIds)
    {
        return mineMapper.deleteUserMineInfos(mineId, userIds);
    }

    /**
     * 新增矿山管理
     * 
     * @param mine 矿山管理
     * @return 结果
     */
    @Override
    public int insertMine(Mine mine)
    {
        Long userId = SecurityUtils.getUserId();
        mine.setCreateBy(String.valueOf(userId));
        mine.setCreateTime(DateUtils.getNowDate());
        return mineMapper.insertMine(mine);
    }

    /**
     * 修改矿山管理
     * 
     * @param mine 矿山管理
     * @return 结果
     */
    @Override
    public int updateMine(Mine mine)
    {
        Long userId = SecurityUtils.getUserId();
        mine.setUpdateBy(String.valueOf(userId));
        mine.setUpdateTime(DateUtils.getNowDate());
        return mineMapper.updateMine(mine);
    }

    /**
     * 批量删除矿山管理
     * 
     * @param ids 需要删除的矿山管理主键
     * @return 结果
     */
    @Override
    public int deleteMineByIds(Long[] ids)
    {
        return mineMapper.deleteMineByIds(ids);
    }

    /**
     * 删除矿山管理信息
     * 
     * @param id 矿山管理主键
     * @return 结果
     */
    @Override
    public int deleteMineById(Long id)
    {
        return mineMapper.deleteMineById(id);
    }
}
